package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.dto.GridMemberDTO;
import com.one.dto.GridMemberLoginDTO;
import com.one.dto.GridMemberPageRequestDTO;
import com.one.dto.PageResponseDto;
import com.one.entity.GridMember;
import com.one.entity.Statistics;
import com.one.entity.Supervisor;
import com.one.exception.BaseException;
import com.one.mapper.GridCityMapper;
import com.one.mapper.GridMemberMapper;
import com.one.mapper.GridProvinceMapper;
import com.one.service.GridMemberService;
import com.one.vo.GridMemberVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class GridMemberServiceImpl implements GridMemberService {
	
	@Autowired
	private GridMemberMapper gridMemberMapper;

	@Autowired
	private GridProvinceMapper gridProvinceMapper;

	@Autowired
	private GridCityMapper gridCityMapper;


	/********************** nepm工程 ***********************/
	@Override
	public List<GridMember> listGridMemberByProvinceId(GridMember gridMember) {
		QueryWrapper<GridMember> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("province_id", gridMember.getProvinceId());
		queryWrapper.eq("city_id", gridMember.getCityId());
		queryWrapper.eq("state", gridMember.getState());
		return gridMemberMapper.selectList(queryWrapper);
	}
	
	/********************** nepm工程 ***********************/
	
	/********************** nepg工程 ***********************/
	@Override
	public GridMember getGridMemberByCodeByPass(GridMemberLoginDTO gridMemberLoginDTO) {
		GridMember gridMemberLogin=gridMemberMapper.getGridMemberByCode(gridMemberLoginDTO);
		if (gridMemberLogin == null) {
			log.error("网格员登录失败，网格员编号或密码错误: {}", gridMemberLoginDTO);
			return null;
		}
		if(!gridMemberLogin.getPassword().equals(gridMemberLoginDTO.getPassword())){
			log.error("网格员登录失败，网格员编号或密码错误: {}", gridMemberLoginDTO);
			return null;
		}
		return gridMemberLogin;
	}
	
	/********************** nepg工程 ***********************/

	@Override
	public int addGridmember(GridMemberDTO gridMemberDTO) {
		Integer provinceId = gridProvinceMapper.getProvinceIdByName(gridMemberDTO.getProvinceName());
		Integer cityId = gridCityMapper.getCityIdByName(gridMemberDTO.getCityName());
		// 3. DTO 转 Entity
		GridMember gridMember = new GridMember();
		gridMember.setGmName(gridMemberDTO.getGmName());
		gridMember.setGmCode(gridMemberDTO.getGmCode());
		gridMember.setProvinceId(provinceId);
		gridMember.setCityId(cityId);
		gridMember.setTel(gridMemberDTO.getTel());
		gridMember.setState(gridMemberDTO.getState());
		gridMember.setRemarks(gridMemberDTO.getRemarks());

		// 4. 设置默认密码（示例）
		gridMember.setPassword("123");

		// 5. 插入数据库
		return gridMemberMapper.insert(gridMember);

	}

	@Override
	public int deleteGridMember(Integer gmId) {
		return gridMemberMapper.deleteById(gmId);
	}

	@Override
	public int updateGridMember(GridMember gridMember) {
		return gridMemberMapper.updateGridmember(gridMember);
	}


	@Override
	public PageResponseDto<GridMemberVO> listGridMember(GridMemberPageRequestDTO request) {
		// 如果页码为null或小于等于0，设置为1
		if (request.getPageNum() == null || request.getPageNum() <= 0) {
			request.setPageNum(1);
		}
		PageResponseDto<GridMemberVO> response = new PageResponseDto<>();

		// 先计算开始查询记录数
		int pageNum = request.getPageNum();
		//TODO 为什么要覆盖，与前端讨论前端传递这个参数意义
		request.setBeginNum((pageNum - 1) * request.getMaxPageNum());

		// 先查询总行数
		int totalRow = gridMemberMapper.countGridMember(request);
		response.setTotalRow(totalRow);

		// 如果总行数为0，那么直接返回
		if (totalRow == 0) {
			response.setPageNum(pageNum);
			response.setMaxPageNum(request.getMaxPageNum());
			response.setList(new ArrayList<>());
			return response;
		}

		// 计算总页数
		int totalPageNum = (totalRow + request.getMaxPageNum() - 1) / request.getMaxPageNum();
		response.setTotalPageNum(totalPageNum);

		// 计算上一页和下一页
		response.setPreNum(pageNum > 1 ? pageNum - 1 : pageNum);
		response.setNextNum(pageNum < totalPageNum ? pageNum + 1 : pageNum);

		// 查询分页数据
		List<GridMemberVO> gridMembers = gridMemberMapper.listGridMembers(request);

		// 给返回值填充余下数据
		response.setPageNum(pageNum);
		response.setMaxPageNum(request.getMaxPageNum());
		response.setBeginNum(request.getBeginNum());
		response.setList(gridMembers);

		return response;
	}
}
